<?php
namespace app\wx\controller;
use think\Db;
class Channel extends Basewx{



    public function add_channel(){
        empty($_GET['employee_num'])&&exit("<script>alert('二维码已经失效请联系业务员');window.location.href='/wx/user/center'</script>");
        $phone = session('wxgzh')['phone'];
        $sales = Db::name('admin_admins')->where([['id','=',$_GET['employee_num']],['status','=','0']])->find();
        !$sales&&exit("<script>alert('二维码已经失效请联系业务员');window.location.href='/wx/user/center'</script>");
        $qd = Db::name('channel_member')->where(['cm_phone'=>$phone])->find();
        if($qd){
            Db::name('channel_member')->where(['cm_phone'=>$phone])->update(['sales_id'=>$sales['id']]);
            exit("<script>alert('业务员资料已经更新成功');window.location.href='/wx/user/center'</script>");
        }
        $this->assign('qd','QD'.time().rand(0,1000));
        $this->assign('sales',$sales);
        $this->assign('data',session('wxgzh'));
        return $this->fetch();
    }



    public function save_channel(){//employee_num
        //dump($_POST);die;
        $phone = session('wxgzh')['phone'];
        $sales = Db::name('admin_admins')->where([['id','=',$_POST['employee_num']],['status','=','0']])->find();
        if(!$sales){
            exit(json_encode(array('code'=>'1','msg'=>'二维码已经失效请联系业务员'),JSON_UNESCAPED_UNICODE));
        }
        $qd = Db::name('channel_member')->where(['cm_phone'=>$phone])->find();
        if($qd){
            Db::name('channel_member')->where(['cm_phone'=>$phone])->update(['sales_id'=>$sales['id']]);
            exit(json_encode(array('code'=>'0','msg'=>'业务员资料更新成功'),JSON_UNESCAPED_UNICODE));
        }
        $acount = Db::name('user_account')->where(['ua_phone'=>$phone])->find();
        if($acount){
            $json=json_decode($acount['ua_type']);
            array_push($json,"4");
            $user_account = [
                'ua_type'    => json_encode($json),
                'ua_status'    => '1',
                'ua_update_time'    => time(),
            ];
            Db::name('user_account')->where(['ua_phone'=>$phone])->update($user_account);
            $userId = $acount['ua_id'];
        }else{
            $user_account = [
                'ua_name'    => session('wxgzh')['nickname'],
                'ua_phone'    => $phone,
                'ua_pwd'    => md5($phone.'123456'),
                'ua_type'    => '[4]',
                'ua_status'    => '1',
                'ua_update_time'    => time(),
                'ua_create_time'    => time(),
            ];
            Db::name('user_account')->insert($user_account);
            $userId = Db::name('user_account')->getLastInsID();
        }
        $qian=array(" ","　","\t","\n","\r");
        $_POST['cm_city'] = str_replace($qian,'',$_POST['cm_city']);
        $data = [
            'cm_name'    => session('wxgzh')['nickname'],
            'cm_head_photo'    => session('wxgzh')['headimgurl'],
            'cm_type'    => $_POST['cm_type'],
            'cm_number'    => 'QD'.time().rand(0,1000),
            'cm_phone'    => $phone,
            'cm_city'    => $_POST['cm_city'],
            'cm_company'    => $_POST['cm_company'],
            'cm_position'    => $_POST['cm_position'],
            'cm_attestation_status'    => '',
            'sales_id'    => $sales['id'],
            'cm_create_time'    => time(),
            'cm_update_time'    => time(),
        ];
        $data['ua_id'] = $userId;
        $res = Db::name('channel_member')->insert($data);
        if($res){
            exit(json_encode(array('code'=>'0','msg'=>'注册成功'),JSON_UNESCAPED_UNICODE));
        }
        exit(json_encode(array('code'=>'1','msg'=>'保存失败'),JSON_UNESCAPED_UNICODE));

    }


    public function add_bank(){
        if($_POST){
            if (empty($_POST['cm_bank_name'])||empty($_POST['bc_id'])||empty($_POST['cm_true_name'])||empty($_POST['cm_bank_address'])||empty($_POST['cm_bank_city'])||empty($_POST['cm_bank_id'])||empty($_POST['cm_user_id'])) {
                exit(json_encode(array('code'=>'1','msg'=>'参数错误'),JSON_UNESCAPED_UNICODE));
            }
            $data = [
                'cm_bank_name'      => $_POST['cm_bank_name'],
                'bc_id'             => $_POST['bc_id'],
                'cm_true_name'      => $_POST['cm_true_name'],
                'cm_bank_address'   => $_POST['cm_bank_address'],
                'cm_bank_city'      => $_POST['cm_bank_city'],
                'cm_bank_id'        => $_POST['cm_bank_id'],
                'cm_user_id'       => $_POST['cm_user_id'],
                'cm_update_time'    => time(),
                //cm_bank_name	varchar(100) NULL	开户银行
                //cm_bank_address	varchar(100) NULL	开户支行
                //cm_bank_city	varchar(20) NULL	开户城市
                //cm_bank_id	varchar(20) NULL	银行卡号
                //cm_user_id	varchar(40) NULL	身份证号码
            ];
            $res = Db::name('channel_member')->where(['ua_id'=>session('wxgzh')['ua_id']])->update($data);
            //$userId = Db::name('channel_member')->getLastInsID();
            if($res){
                exit(json_encode(array('code'=>'0','msg'=>'注册成功'),JSON_UNESCAPED_UNICODE));
            }
            exit(json_encode(array('code'=>'1','msg'=>'保存失败'),JSON_UNESCAPED_UNICODE));
        }
        $qd = Db::name('channel_member')->where(['cm_phone'=>session('wxgzh')['phone']])->find();
        !$qd&&exit("<script>alert('请联系业务员注册成为渠道商');window.location.href='/wx/user/center'</script>");
        $qd['cm_type']==1&&$res['cm_type']='房产中介';
        $qd['cm_type']==2&&$res['cm_type']='个人';
        $qd['cm_type']==3&&$res['cm_type']='转介绍';
        $this->assign('res',$qd);
        return $this->fetch();
    }

    //我的邀请列表
    public function recommend(){

    }

    //渠道添加房源
    public function add_apartment(){
        if($_POST){
            $qd = Db::name('channel_member')->where(['cm_phone'=>session('wxgzh')['phone']])->find();
            if(empty($_POST['hr_owner'])||empty($_POST['hr_owner_phone'])||empty($_POST['hr_city'])||empty($_POST['hr_rq_id'])||empty($_POST['hr_bc_id'])||empty($_POST['hr_area_id'])){
                exit(json_encode(array('code'=>'1','msg'=>'必填参数为空'),JSON_UNESCAPED_UNICODE));
            }
            $data = [
                'hr_serial_number'  => 'TY'.date("YmdHis",time()).mt_rand(10,99),
                'hr_sales_id'       => $qd['sales_id'],
                'hr_cm_id'          => $qd['cm_id'],
                'hr_owner'          => $_POST['hr_owner'],
                'hr_owner_phone'    => $_POST['hr_owner_phone'],
                'hr_rq_id'          => $_POST['hr_rq_id'],
                'hr_bc_id'          => $_POST['hr_bc_id'],
                'hr_city'           => $_POST['hr_city'],
                'hr_area_id'        => $_POST['hr_area_id'],
                'hr_house_area'     => !$_POST['hr_house_area']?null:$_POST['hr_house_area'],
                'hr_room_number'    => !$_POST['hr_room_number']?null:$_POST['hr_room_number'],
                'hr_parlour_number' => !$_POST['hr_parlour_number']?null:$_POST['hr_parlour_number'],
                'hr_toilet_number'  => !$_POST['hr_toilet_number']?null:$_POST['hr_toilet_number'],
                'hr_create_time'    => time(),
                'hr_update_time'    => time(),
                //hr_city	varchar(255) NULL	所属城市 省市区
                //hr_rq_id	int(11) NULL	小区名称 绑定关联小区表ID
                //hr_house_number	varchar(255) NULL	门牌号
                //hr_owner	varchar(255) NULL	业主姓名
                //hr_owner_phone	varchar(255) NULL	业主手机号
                //hr_house_area	varchar(255) NULL	房源面积
                //hr_room_number	int(11) NULL	居室数量
                //hr_parlour_number	int(11) NULL	客厅数量
                //hr_toilet_number	int(11) NULL	卫生间数量
                //hr_rq_id	int(11) NULL	小区名称 绑定关联小区表ID
                //hr_bc_id	int(11) NULL	所属商圈 关联商圈表ID
            ];
            $res = Db::name('housing_resources')->insert($data);
            //$userId = Db::name('channel_member')->getLastInsID();
            if($res){
                exit(json_encode(array('code'=>'0','msg'=>'注册成功'),JSON_UNESCAPED_UNICODE));
            }
            exit(json_encode(array('code'=>'1','msg'=>'保存失败'),JSON_UNESCAPED_UNICODE));
        }
        $qd = Db::name('channel_member')->where(['cm_phone'=>session('wxgzh')['phone']])->find();
        !$qd&&exit("<script>alert('请联系业务员注册成为渠道商');window.location.href='/wx/user/center'</script>");
        return $this->fetch();
    }

    public function channel_house(){
        return $this->fetch();
    }


    public function jump(){
        return $this->fetch();
    }


    public function my_manager(){
        $qd = Db::name('channel_member')->where(['cm_phone'=>session('wxgzh')['phone']])->find();
        !$qd&&exit("<script>alert('请联系业务员注册成为渠道商');window.location.href='/wx/user/center'</script>");
        $sales = Db::name('admin_admins')->where(['id'=>$qd['sales_id']])->find();
        $this->assign('sales',$sales);
        return $this->fetch();
    }


    public function record(){
        $qd = Db::name('channel_member')->where(['cm_phone'=>session('wxgzh')['phone']])->find();
        $sales = Db::name('admin_admins')->where(['id'=>$qd['sales_id']])->find();
        $this->assign('sales',$sales);
        return $this->fetch();
    }



    public function upload(){
        $this->Tool->upload_file('channel_head');
    }




}
